/**
 * 这里定义了需要经常使用的通用函数，方便代码复用
 * @author 程文智
 */
package com.taxsystem.tool;

import java.io.InputStream;
import java.util.Scanner;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class CommonOperations {
	private static final String TAG ="CommonOperators";

	/**
	 * convert InputStream to String
	 * @author 程文智
	 */
	public static String inputStream2String(InputStream in){
		//根据开发文档，服务器返回消息使用UTF-8编码
		Scanner scanner = new Scanner(in,"UTF-8");
		String str = null;
		str=scanner.nextLine();
		while(scanner.hasNext()){
			str+=scanner.nextLine();
		}
		Log.v(TAG, "inputStream2String(): convert to String:"+str);
		return str;
	}
	
	/**
	 * 判断设备网络开关是否打开。并不意味着网络正常。
	 * @param activity 当前的Activity
	 * @author 程文智
	 */
	public static boolean isNetworkConnected(Activity activity){
		ConnectivityManager connMgr = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnected()) {
			Log.v(TAG, "network connect!");
			return true;
		} else {
			Log.w(TAG,
					"network do not connect!");
			return false;
		}
	}
}
